﻿using Comm.Model;
using Comm.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Comm.Web.Action
{
    /// <summary>
    /// orderComfirm 的摘要说明
    /// </summary>
    public class orderComfirm : BaseHander
    {
        BLL.AreaService bllarea = new BLL.AreaService();
        BLL.CheckAddressService blladdress = new BLL.CheckAddressService();
        public override void SonLoad()
        {
            string type = context.Request.Params["type"];
            switch (type) 
            {
                case "paymoney":
                    ParGo();
                    break;
                case "area":
                    GetArea();
                    break;
                case "address":
                    DoAddress();
                    break;
                case "mraddress":
                    getAddress();
                    break;
            }
        }

        public void ParGo()
        {
            int userid = UserNow.uId;
            string number = DateTime.Now.ToString("yyyyMMddHHmmssfff") + userid;//订单号。
            string address = string.Format("收件人:{0},电话:{1},地址:{2},邮编:{3}", Request.Form["name"], Request.Form["phone"], Request.Form["address"], Request.Form["postCode"]);
            BLL.OrdersService bllorder = new BLL.OrdersService();
            decimal totalMoney = bllorder.CreateOrders(userid, number, address);//下订单获取该订单的总价
            AjaxMsgHelper.AjaxMsg("ok", "支付成功！~~~", JsonHelper.Serialize(totalMoney));
        }

        public void GetArea() 
        {
           IList<Area> list =bllarea.GetArea();
           AjaxMsgHelper.AjaxMsg("ok","数据获取成功！~~",JsonHelper.Serialize(list));
        }

        public void DoAddress() 
        {
            Model.CheckAddress model = new CheckAddress() 
            {
                 UserId=UserNow.uId,
                 UserName = context.Request.Params["name"],
                 Address = context.Request.Params["address"],
                 postCode = context.Request.Params["postCode"],
                 TelPhone = context.Request.Params["phone"],
                 statu = false                
            };
           int Id = blladdress.Insert(model);
           Model.CheckAddress model1 = new CheckAddress()
           {              
               Id=Id,
               UserId = UserNow.uId,
               UserName = context.Request.Params["name"],
               Address = context.Request.Params["address"],
               postCode = context.Request.Params["postCode"],
               TelPhone = context.Request.Params["phone"],
               statu = false
           };
            AjaxMsgHelper.AjaxMsg("ok","添加成功！~~",JsonHelper.Serialize(model1));
        }

        public void getAddress()
        {
            int id = int.Parse(Request.Form["id"]);
            blladdress.Update1(id);
            CheckAddress model = blladdress.QuerySingle1(id);
            AjaxMsgHelper.AjaxMsg("ok", "设置成功！~~~", JsonHelper.Serialize(model));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}